import * as cesium from 'cesium/Cesium'
const Cesium = cesium

class Draw {
    constructor(oneGisCesium) {
        this.oneGisCesium = oneGisCesium;
        this.viewer = oneGisCesium.viewer;
        this.finalShape = [];
        this.activeShapePoints = [];
        this.activeShape = undefined;
        this.drawingMode = '';
        this.finalPoints = [];
        this.handler = null
        this.callback = () => {};
    }
    creatUnitPoint() {
        let viewer = this.viewer;
        this.handler && this.handler.destroy();
        this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        //鼠标左键
        this.handler.setInputAction(event => {
            let earthPosition = this.oneGisCesium.convert.getPickPosition(event.position);
            if (Cesium.defined(earthPosition)) {
                let point = this.createPoint(earthPosition);
                this.finalShape.push(point);
                this.handler.destroy();
                this.callback(earthPosition, {
                    finishListen: true,
                    entities: this.finalShape
                });
                
            }
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    }
    createPoint(worldPosition) {
        let viewer = this.viewer;
        let point = viewer.entities.add({
            position: worldPosition,
            point: {
                color: Cesium.Color.RED,
                pixelSize: 10,
                disableDepthTestDistance: Number.POSITIVE_INFINITY
            }
        });
        return point;
    }
    drawShape(positionData) {
        let shape;
        let drawingMode = this.drawingMode;
        let viewer = this.viewer;
        if (drawingMode === 'polyline') {
            shape = viewer.entities.add({
                polyline: {
                    positions: positionData,
                    material: Cesium.Color.RED,
                    depthFailMaterial: Cesium.Color.RED,
                    width: 3
                }
            });
        } else if (drawingMode === 'tdpolyline') {
            shape = viewer.entities.add({
                polyline: {
                    positions: positionData,
                    clampToGround: true,
                    material: Cesium.Color.RED,
                    width: 3
                }
            });

        } else if (drawingMode === 'polygon') {
            shape = viewer.entities.add({
                polygon: {
                    hierarchy: positionData,
                    material: new Cesium.ColorMaterialProperty(Cesium.Color.RED.withAlpha(0.7))
                }
            });
        } else if (drawingMode === 'tdpolygon') {
            shape = viewer.entities.add({
                polygon: {
                    hierarchy: positionData,
                    material: new Cesium.ColorMaterialProperty(Cesium.Color.RED.withAlpha(0.7)),
                    perPositionHeight:true
                }
            });
        }
        return shape
    }
    handleDraw() {
        this.handler && this.handler.destroy();
        let viewer = this.viewer;
        this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        let activeShapePoints = this.activeShapePoints;
        let drawingMode = this.drawingMode;
        //鼠标左键
        this.handler.setInputAction(event => {
            let earthPosition = this.oneGisCesium.convert.getPickPosition(event.position);
            this.finalPoints.push(JSON.parse(JSON.stringify(earthPosition)))
            if (Cesium.defined(earthPosition)) {
                if (activeShapePoints.length === 0) {
                    activeShapePoints.push(earthPosition);
                    var dynamicPositions = new Cesium.CallbackProperty(() => {
                        if (drawingMode === 'polygon'||drawingMode === 'tdpolygon') {
                            return new Cesium.PolygonHierarchy(activeShapePoints);
                        }
                        return activeShapePoints;
                    }, false);
                    this.activeShape = this.drawShape(dynamicPositions); //绘制动态图
                }
                activeShapePoints.push(earthPosition);
                let point = this.createPoint(earthPosition);
                this.finalShape.push(point);
                this.callback(this.activeShapePoints)
            }
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        //鼠标移动
        this.handler.setInputAction(event => {
            if (Cesium.defined(this.activeShape)) {
                let newPosition = this.oneGisCesium.convert.getPickPosition(event.endPosition);
                if (Cesium.defined(newPosition)) {
                    activeShapePoints.pop();
                    activeShapePoints.push(newPosition);
                    this.callback(this.activeShapePoints)
                }
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        this.handler.setInputAction(event => {
            if((this.drawingMode=="polygon"||this.drawingMode=="tdpolygon")&&this.finalPoints.length>2){
                this.terminateShape();
                this.handler.destroy();
            }else if((this.drawingMode=="polyline"||this.drawingMode=="tdpolyline")&&this.finalPoints.length>1){
                this.terminateShape();
                this.handler.destroy();
            }
            

        }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
    }
    terminateShape() {
        let viewer = this.viewer;
        this.activeShapePoints.pop(); //去除最后一个动态点
        if (this.activeShapePoints.length) {
            let finalShape = this.drawShape(this.activeShapePoints); //绘制最终图
            this.finalShape.push(finalShape);
            console.log(this.oneGisCesium.convert.cartesian3ToDegreesArray(this.finalPoints))
            this.callback(this.finalPoints, {
                finishListen: true,
                entities: this.finalShape
            })
        }
        viewer.entities.remove(this.activeShape); //去除动态图形
        this.activeShape = undefined;
        this.activeShapePoints = [];
        this.finalPoints = []
    }
    point(callback) {
        this.createShape('point', callback);
    }
    polyline(callback) {
        this.createShape('polyline', callback);
    }
    polygon(callback) {
        this.createShape('polygon', callback);
    }
    createShape(type, callback) {
        this.drawingMode = type;
        if (type === 'point') {
            this.creatUnitPoint();
        } else {
            this.handleDraw();
        }
        if (!callback) return;
        this.callback = callback;
    }
    clear() {
        for (let shape of this.finalShape) {
            this.viewer.entities.remove(shape)
        }
        this.viewer.entities.remove(this.activeShape);
        this.activeShape = undefined;
        this.activeShapePoints = [];
        this.handler && this.handler.destroy();
        this.finalShape = [];
    }

}

export default Draw